# @author zfd
# @date 2022年12月13日 16:38
# @desc 学生信息系统


# 存放信息的文件
import os

INFO_FILE_PATH = 'students.txt'


def check_file():
    if not os.path.exists(INFO_FILE_PATH):
        with open(INFO_FILE_PATH, 'w') as f:
            f.write('')


def sayBye():
    print('byebye!')


# 覆盖的方式写入
def write_all_students(students):
    with open(INFO_FILE_PATH, 'w', encoding='UTF-8') as f:
        for s in students:
            f.writelines(str(s) + '\n')


# 追加的方式写入
def append_all_students(students):
    with open(INFO_FILE_PATH, 'a', encoding='UTF-8') as f:
        for s in students:
            f.writelines(str(s) + '\n')


# 从文件中读取所有学生
def read_all_students():
    students = []
    with open(INFO_FILE_PATH, 'r', encoding='UTF-8') as f:
        lines = f.readlines()
        for line in lines:
            students.append(dict(eval(line)))
    return students


def doInsert():
    students = []
    while True:
        stu_no = int(input('ID: '))
        name = input('Name: ')
        cs = int(input('C: '))
        js = int(input('Java: '))
        ps = int(input('Python: '))
        students.append({'id': stu_no, 'name': name, 'C': cs, 'Java': js, 'Python': ps})
        ct = input('continue to add ? y/n :')
        if ct == 'n':
            break
    # 写入到文件
    append_all_students(students)
    print('add finished!')


# 2.查找学生信息
def doFind():
    students = read_all_students()
    if len(students) > 0:
        no = int(input('请输入学生ID: '))
        for stu in students:
            if (stu['id'] == no):
                print(stu)
        # 是否继续
        ct = input('continue to find ? y/n :')
        if ct == 'y':
            doFind()

    else:
        print('当前系统暂无学生信息可查询')


def doDel():
    students = read_all_students()
    deleted = False
    if len(students) > 0:
        while True:
            no = int(input('请输入学生ID: '))
            for stu in students:
                if (stu['id'] == no):
                    students.remove(stu)
                    deleted = True
            ct = input('continue to delete ? y/n :')
            if ct == 'n':
                break
        if deleted:
            print(stu)
            write_all_students(students)
            print('delete finished!')

    else:
        print('当前系统暂无学生信息')


def doModify():
    students = read_all_students()
    modify_lst = []
    if len(students) > 0:
        no = int(input('请输入学生ID: '))
        for stu in students:
            if (stu['id'] == no):
                students.remove(stu)
                modify_lst.append(stu)
        if len(modify_lst) > 0:
            print(f'找到{len(modify_lst)}条符合ID的记录，现在可修改')
            for stu in modify_lst:
                stu['name'] = input('name: ')
                stu['C'] = int(input('C: '))
                stu['Java'] = int(input('Java: '))
                stu['Python'] = int(input('Python: '))
            #  写入
            students.extend(modify_lst)
            write_all_students(students)
            print('modify finished!')
            ct = input('continue to modify? y/n :')
            if ct == 'y':
                doModify()


def doSort():
    students = read_all_students()
    if len(students) > 0:
        ct = input('总成绩升序或是降序? 1/0 ?')
        reverse_sort = False
        if ct == '1':
            reverse_sort = False
        else:
            reverse_sort = True
        students.sort(key=lambda x: x['C'] + x['Java'] + x['Python'], reverse=reverse_sort)
        # 标题
        print('%-10s %-10s %-10s %-10s %-10s %-10s' % ('ID', 'Name', 'C', 'Java', 'Python', '总成绩'))
        for stu in students:
            print('%-10d %-10s %-10.3f %-10.3f %-10.3f %-10.3f' % (
                stu['id'], stu['name'], stu['C'], stu['Java'], stu['Python'],
                stu['C'] + stu['Java'] + stu['Python']))


def doCount():
    students = read_all_students()
    print(f'学生的总数是 {len(students)}')


def doShowAll():
    students = read_all_students()
    # 标题
    print('ID'.ljust(10), '名字'.ljust(10), 'C成绩'.ljust(10), 'Java成绩'.ljust(10), 'Python成绩'.ljust(10))
    for stu in students:
        # print(str(stu['id']).ljust(10), stu['name'].ljust(10), str(stu['C']).ljust(10), str(stu['Java']).ljust(10),
        #       str(stu['Python']).ljust(10))
        print('%10d %10s %10.3f %10.3f %10.3f' % (stu['id'], stu['name'], stu['C'], stu['Java'], stu['Python']))
        print('')


def main():
    # 检查文件
    check_file()
    while True:
        # 菜单提示
        menu()
        ch = int(input('请选择： '))
        if ch == 0:
            sayBye()
            break;
        elif ch == 1:
            doInsert()
        elif ch == 2:
            doFind()
        elif ch == 3:
            doDel()
        elif ch == 4:
            doModify()
        elif ch == 5:
            doSort()
        elif ch == 6:
            doCount()
        elif ch == 7:
            doShowAll()


def menu():
    print('===========学生信息管理系统=============')
    print('----------------功能菜单---------------')
    print('\t\t\t', '1.录入学生信息')
    print('\t\t\t', '2.查找学生信息')
    print('\t\t\t', '3.删除学生信息')
    print('\t\t\t', '4.修改学生信息')
    print('\t\t\t', '5.排序')
    print('\t\t\t', '6.统计学生总数')
    print('\t\t\t', '7.显示所有学生信息')
    print('\t\t\t', '0.退出系统')

    pass


if __name__ == '__main__':
    main()
